package com.example.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.auth.domain.DataNode;
import com.example.config.auth.SecurityUtil;
import com.example.feign.UserFeignService;
import com.example.system.domain.BusSystemReceivemsg;
import com.example.util.Results;
import com.example.util.sql.SqlGenerator;
import com.example.util.sql.SqlResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

@RequestMapping("/test")
@RestController
public class TestController {

    @Value("${spring.application.name:defaultServer}")
    private String serverName;

    @Autowired
    private UserFeignService userFeignService;

    @GetMapping("/info")
    public JSONObject testController(){
        BusSystemReceivemsg busSystemReceivemsg = new BusSystemReceivemsg();
        busSystemReceivemsg.setUuid(UUID.randomUUID().toString());
        SqlResult sqlResult=SqlGenerator.generateInsert("bus-table",busSystemReceivemsg);
        return Results.success(sqlResult);
    }

    @GetMapping("/getnode")
    public JSONObject getUserNode(){
        String userName=SecurityUtil.getCurrentUsername();
        DataNode nodeConfig=userFeignService.findNodeByUsername(userName);
        return Results.success(nodeConfig);
    }

    @GetMapping("/serverName")
    public JSONObject getServerName(){
        return Results.success(serverName);
    }

    @GetMapping("/getuserinfo")
    public JSONObject getUserInfo(){
        String userId=SecurityUtil.getUserId();
        Authentication authentication=SecurityUtil.getCurrentAuthentication();
        return Results.success(authentication);
    }
}
